package vikaa.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringEscapeUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.vikaa.wowo.widget.PullToRefreshListView;
import com.vikaa.wowo.widget.PullToRefreshListView.OnRefreshListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import vikaa.timelinepackage.Timeline;
import vikaa.tools.HttpUtil;
import vikaa.tools.Logger;
import vikaa.wowo.R;
import vikaa.wowo.cache.DefaultValueSharePreference;
import vikaa.wowo.cache.InvitorOpenHelper;
import vikaa.wowo.cache.SpaceListSqlStoragePrivate;
import vikaa.wowo.cache.UserInfoSharePreference;
import vikaa.adapter.MySpaceListAdapter;
import vikaa.bean.Space;
import vikaa.bean.SpaceOauth;

public class Mywowo extends Activity implements OnItemClickListener{
	private ImageView settingsBtn;
	private ImageView moreBtn;
	private PullToRefreshListView wowolistview;
	private MySpaceListAdapter mAdapter;
	private ArrayList<Space> wowoList = new ArrayList<Space>() ;
	public static ArrayList<Space> mList;
	private long exitTime = 0;
	private int menuGroup;
	private static final int SETTINGS_GROUP = 1;
	private static final int MORE_GROUP = 2;
	private static String uid;
	public static boolean isrefresh = false;
	public static boolean crashflag = false;
	public static boolean wowoexit = false;
	private RelativeLayout lookaroundBtn;
	
	private LinearLayout header;
	private	RelativeLayout guideRl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mywowo);
		DefaultValueSharePreference.putLoginFlag(Mywowo.this, true);
		Intent i = getIntent();
		uid = i.getExtras().getString("uid");
		if(UserInfoSharePreference.getCrash(Mywowo.this).equals("1")){
			finish();
		}
		mList = new ArrayList<Space>();
		initview();
		wowolistview.state = wowolistview.REFRESHING;
		wowolistview.changeHeaderViewByState();
		new downMywowoThread(uid).start();
		if(!DefaultValueSharePreference.getguidewowo(Mywowo.this)){
			guideRl = (RelativeLayout)findViewById(R.id.guidewowo);
			guideRl.setVisibility(View.VISIBLE);
			guideRl.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					guideRl.setVisibility(View.GONE);
					DefaultValueSharePreference.putguidewowo(Mywowo.this, true);
				}
			});
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		refresh();
		if(crashflag){
			finish();
			crashflag = false;
			//mList.clear();
			//startActivity(new Intent(Mywowo.this,Welcome.class));
		}
		if(mList.size() != 0){
			for(int i =0;i<mList.size();i++){
				mList.get(i).setState(SpaceListSqlStoragePrivate.getState(mList.get(i).getSpace_id(), Mywowo.this));
				mList.get(i).setSpacename(SpaceListSqlStoragePrivate.getSpaceName(mList.get(i).getSpace_id(), Mywowo.this));
			}
			mAdapter.notifyDataSetChanged();
		}
	}
	@Override
	protected void onDestroy() {
		//PushService.actionStop(getApplicationContext());
		super.onDestroy();
		mList.clear();
		Mywowo.this.deleteDatabase(InvitorOpenHelper.DB_NAME);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN ){     
	        if((System.currentTimeMillis()- exitTime) > 2000){    
	            Toast.makeText(getApplicationContext(), getString(R.string.exit_toast), Toast.LENGTH_SHORT).show();                                  
	            exitTime = System.currentTimeMillis();     
	        } else {  
	            finish();  
	            //System.exit(0);  
	        }  
	        return true;     
	    }  
	    return super.onKeyDown(keyCode, event); 
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		this.getMenuInflater().inflate(R.menu.mywowo_menu, menu);
		menu.setGroupVisible(R.id.group_more, false);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		switch(menuGroup){
		case SETTINGS_GROUP:
			menu.setGroupVisible(R.id.group_more, false);
			menu.setGroupVisible(R.id.group_settings, true);
			break;
		case MORE_GROUP:
			menu.setGroupVisible(R.id.group_settings, false);
			menu.setGroupVisible(R.id.group_more, true);
			break;
		default:
			menu.setGroupVisible(R.id.group_more, false);
			menu.setGroupVisible(R.id.group_settings, true);
			break;
		}
		return super.onPrepareOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case R.id.menu_exit:
			finish();
			break;
		case R.id.menu_creatwowo:
			startActivity(new Intent(Mywowo.this,WowoCreate1.class));
			break;
		case R.id.settings_user:
			startActivity(new Intent(Mywowo.this,SettingsUser.class));
			break;
		/*case R.id.menu_refresh:
			isrefresh = true;
			refresh();
			break;*/
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void openOptionsMenu() {
		super.openOptionsMenu();
	}
	
	
	private void refresh(){
		if(isrefresh){
			isrefresh = false;
			Logger.i(""+ wowoexit);
			if(wowoexit){
				Toast.makeText(Mywowo.this, getString(R.string.msg_logout_wowo_success), Toast.LENGTH_SHORT).show();
				wowoexit = false;
			}
			//DialogService.showProgressDialog(Mywowo.this, "刷新窝窝列表");
			//header.setVisibility(View.VISIBLE);
			wowolistview.state = wowolistview.REFRESHING;
			wowolistview.changeHeaderViewByState();
			//mList.clear();
			new refreshMywowoThread(uid,8).start();
		}
	}
	public void initview(){
		settingsBtn = (ImageView)findViewById(R.id.settingsBtn);
		settingsBtn.setOnClickListener(new clickListener());
		moreBtn = (ImageView)findViewById(R.id.moreBtn);
		moreBtn.setOnClickListener(new clickListener());
		lookaroundBtn = (RelativeLayout)findViewById(R.id.lookaound);
		lookaroundBtn.setOnClickListener(new clickListener());
		/*settingsBtn.setClickable(false);
		moreBtn.setClickable(false);
		lookaroundBtn.setClickable(false);*/
		wowolistview = (PullToRefreshListView) findViewById(R.id.wowolist);
		wowolistview.setOnItemClickListener(this);
		
		mAdapter = new MySpaceListAdapter(Mywowo.this, mList);
        wowolistview.setAdapter(mAdapter);
        header = (LinearLayout)findViewById(R.id.header);
        header.setVisibility(View.GONE);
        wowolistview.setOnRefreshListener(new OnRefreshListener() {
			public void onRefresh() {
				SpaceListSqlStoragePrivate.dropSpaceList(Mywowo.this);
				new refreshMywowoThread(uid,9).start();
			}
		});
	}
	Handler mhandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
			case 1:
				header.setVisibility(View.GONE);
				settingsBtn.setClickable(true);
				moreBtn.setClickable(true);
				lookaroundBtn.setClickable(true);
				wowolistview.state = wowolistview.DONE;
				wowolistview.changeHeaderViewByState();
				if(msg.obj != null){
					ArrayList<Space> strings = (ArrayList<Space>) msg.obj;
					if(!strings.isEmpty()){
						mList.clear();
						SpaceListSqlStoragePrivate.saveSpaceList(strings, uid, Mywowo.this);
						mList.addAll(SpaceListSqlStoragePrivate.getSpaceList(uid, Mywowo.this));
						mAdapter.notifyDataSetChanged();
					}
				}
				if(mList.size() == 0){
					startActivity(new Intent(Mywowo.this,WowoCreate1.class));
				}
				break;
			case 2:
				break;
			case 3://info = null
				wowolistview.state = wowolistview.DONE;
				wowolistview.changeHeaderViewByState();
				startActivity(new Intent(Mywowo.this,WowoCreate1.class));
				break;
			case 4://access error
				wowolistview.state = wowolistview.DONE;
				wowolistview.changeHeaderViewByState();
				mList.clear();
				mList = SpaceListSqlStoragePrivate.getSpaceList(uid, Mywowo.this);
				mAdapter.notifyDataSetChanged();
				header.setVisibility(View.GONE);
				if(msg.obj != null){
					Toast.makeText(Mywowo.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
				}
				else
					Toast.makeText(Mywowo.this, R.string.server_crash, Toast.LENGTH_SHORT).show();
				break;
			case 5://network error
				wowolistview.state = wowolistview.DONE;
				wowolistview.changeHeaderViewByState();
				mList.clear();
				mList = SpaceListSqlStoragePrivate.getSpaceList(uid, Mywowo.this);
				mAdapter.notifyDataSetChanged();
				header.setVisibility(View.GONE);
				Toast.makeText(Mywowo.this, getString(R.string.network_crash), Toast.LENGTH_SHORT).show();
				break;
			case 0://refresh fail
				//DialogService.closeProgressDialog();
				//header.setVisibility(View.GONE);
				wowolistview.state = wowolistview.DONE;
				wowolistview.changeHeaderViewByState();
				isrefresh = false;
				Toast.makeText(Mywowo.this, getString(R.string.info_msg2), Toast.LENGTH_SHORT).show();
				break;
			case 8://refresh
				//DialogService.closeProgressDialog();
				//header.setVisibility(View.GONE);
				wowolistview.state = wowolistview.DONE;
				wowolistview.changeHeaderViewByState();
				if(msg.obj != null){
					ArrayList<Space> strings = (ArrayList<Space>) msg.obj;
					if(!strings.isEmpty()){
						Logger.i(strings.size()+"");
						mList.clear();
						SpaceListSqlStoragePrivate.dropSpaceList(Mywowo.this);
						SpaceListSqlStoragePrivate.saveSpaceList(strings, uid, Mywowo.this);
						mList.addAll(SpaceListSqlStoragePrivate.getSpaceList(uid, Mywowo.this));
						mAdapter.notifyDataSetChanged();
						wowolistview.setSelection(0);
					}
				}
				if(mList.size() == 0){
					startActivity(new Intent(Mywowo.this,WowoCreate1.class));
				}
				break;
			case 9:
				if(msg.obj != null){
					ArrayList<Space> strings = (ArrayList<Space>) msg.obj;
					if(!strings.isEmpty()){
						mList.clear();
						SpaceListSqlStoragePrivate.saveSpaceList(strings, uid, Mywowo.this);
						mList.addAll(SpaceListSqlStoragePrivate.getSpaceList(uid, Mywowo.this));
						mAdapter.notifyDataSetChanged();
						wowolistview.setSelection(0);
					}
				}
				if(mList.size() == 0){
					startActivity(new Intent(Mywowo.this,WowoCreate1.class));
				}
				wowolistview.onRefreshComplete();
				break;
			}
		}
		
	};
	class clickListener implements OnClickListener{
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId()){
			case R.id.settingsBtn:
				menuGroup = SETTINGS_GROUP;
				openOptionsMenu();
				break;
			case R.id.moreBtn:
				startActivity(new Intent(Mywowo.this,WowoCreate1.class));
				break;
			case R.id.lookaound:
				startActivity(new Intent(Mywowo.this,WowoRecommand.class));
				break;
			}
		}
	}
	class downMywowoThread extends Thread{
		private String uid;
		private boolean isrun = true;
		public downMywowoThread(String uid) {
			super();
			this.uid = uid;
		}
		@Override
		public void run() {
			while(isrun){
				isrun =false;
			Map<String, String> map = new HashMap<String, String>();
			map.put("uid", uid);
			map.put("lastrequest", DefaultValueSharePreference.getLastRequest(Mywowo.this));
			DefaultValueSharePreference.putLastRequest(Mywowo.this, String.valueOf(System.currentTimeMillis()/1000));
			String url = HttpUtil.BASE_URL + "space/list";
			try {
				JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(url, map));
				String result = jsonObj.getString("result");
				if(result.equals("1")){
					if(jsonObj.get("info").toString().length()>3){
						JSONArray mySpacelist =  jsonObj.getJSONObject("info").getJSONArray("mySpace");
						JSONArray joinedSpacelist =  jsonObj.getJSONObject("info").getJSONArray("joinedSpace");
						JSONArray invitedSpacelist =  jsonObj.getJSONObject("info").getJSONArray("invitedSpace");
						/*Logger.i(mySpacelist.toString());
						Logger.i(joinedSpacelist.toString());*/
						//Logger.i(invitedSpacelist.toString());
						wowoList = new ArrayList<Space>();
						if(invitedSpacelist.length()>0){
							for(int i=0;i<invitedSpacelist.length();i++){
								Space list = new Space();
								list.setSpacetype("invitedSpace");
								list.setCount(invitedSpacelist.getJSONObject(i).getString("twitterCount"));
								list.setNumber(invitedSpacelist.getJSONObject(i).getString("memberCount"));
								list.setManagerCount(invitedSpacelist.getJSONObject(i).getString("managerCount"));
								list.setSpace_id(invitedSpacelist.getJSONObject(i).getString("space_id"));
								list.setNewMsg(invitedSpacelist.getJSONObject(i).getString("new"));
								list.setSpacename(StringEscapeUtils.unescapeHtml4(invitedSpacelist.getJSONObject(i).getString("spacename")));
								String cover = invitedSpacelist.getJSONObject(i).getString("cover");
								list.setCover(cover);
								list.setState(invitedSpacelist.getJSONObject(i).getString("state"));
								ArrayList<SpaceOauth> solist = new ArrayList<SpaceOauth>();
								for(int j =0;j<invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").length();j++){
									SpaceOauth so = new SpaceOauth();
									so.setNickname_fromuser(invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getJSONObject("from_user").getString("nickname"));
									so.setUid(invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("uid"));
									so.setCredits(invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("credits"));
									so.setRole(invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("role"));
									so.setCallname(invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("callname"));
									solist.add(so);
									}
								list.setSpaceoauth(solist);
								wowoList.add(list);
							}
						}
						if(mySpacelist.length()>0){
							for(int i=0;i<mySpacelist.length();i++){
								Space list = new Space();
								list.setSpacetype("mySpace");
								list.setCount(mySpacelist.getJSONObject(i).getString("twitterCount"));
								list.setNumber(mySpacelist.getJSONObject(i).getString("memberCount"));
								list.setManagerCount(mySpacelist.getJSONObject(i).getString("managerCount"));
								list.setTotalMemberCount(String.valueOf(Integer.valueOf(list.getNumber())+Integer.valueOf(list.getManagerCount())));
								list.setSpace_id(mySpacelist.getJSONObject(i).getString("space_id"));
								list.setNewMsg(mySpacelist.getJSONObject(i).getString("new"));
								list.setSpacename(StringEscapeUtils.unescapeHtml4(mySpacelist.getJSONObject(i).getString("spacename")));
								String cover = mySpacelist.getJSONObject(i).getString("cover");
								list.setCover(cover);
								list.setState(mySpacelist.getJSONObject(i).getString("state"));
								ArrayList<SpaceOauth> solist = new ArrayList<SpaceOauth>();
								for(int j =0;j<mySpacelist.getJSONObject(i).getJSONArray("spaceoauth").length();j++){
									SpaceOauth so = new SpaceOauth();
									so.setUid(mySpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("uid"));
									so.setCredits(mySpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("credits"));
									so.setRole(mySpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("role"));
									so.setCallname(mySpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("callname"));
									solist.add(so);
									}
								list.setSpaceoauth(solist);
								wowoList.add(list);
							}	
						}
						if(joinedSpacelist.length()>0){
							for(int i=0;i<joinedSpacelist.length();i++){
								Space list = new Space();
								list.setSpacetype("joinedSpace");
								list.setCount(joinedSpacelist.getJSONObject(i).getString("twitterCount"));
								list.setNumber(joinedSpacelist.getJSONObject(i).getString("memberCount"));
								list.setManagerCount(joinedSpacelist.getJSONObject(i).getString("managerCount"));
								list.setTotalMemberCount(String.valueOf(Integer.valueOf(list.getNumber())+Integer.valueOf(list.getManagerCount())));
								list.setSpace_id(joinedSpacelist.getJSONObject(i).getString("space_id"));
								list.setNewMsg(joinedSpacelist.getJSONObject(i).getString("new"));
								list.setSpacename(StringEscapeUtils.unescapeHtml4(joinedSpacelist.getJSONObject(i).getString("spacename")));
								String cover = joinedSpacelist.getJSONObject(i).getString("cover");
								list.setCover(cover);
								list.setState(joinedSpacelist.getJSONObject(i).getString("state"));
								ArrayList<SpaceOauth> solist = new ArrayList<SpaceOauth>();
								for(int j =0;j<joinedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").length();j++){
									SpaceOauth so = new SpaceOauth();
									so.setUid(joinedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("uid"));
									so.setCredits(joinedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("credits"));
									so.setRole(joinedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("role"));
									so.setCallname(joinedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("callname"));
									solist.add(so);
									}
								list.setSpaceoauth(solist);
								wowoList.add(list);
							}
						}
						Message msg = mhandler.obtainMessage(1);
						msg.obj = wowoList;
						msg.sendToTarget();
					}
					else{
						Message msg = mhandler.obtainMessage(3);
						msg.sendToTarget();
					}
				}
				else if(result.equals("0")){
					Message msg = mhandler.obtainMessage(4);
					msg.obj = jsonObj.getString("msg");
					msg.sendToTarget();
				}
				else{
					Message msg = mhandler.obtainMessage(5);
					msg.obj = jsonObj.getJSONObject("info").getString("error");
					msg.sendToTarget();
				}
			} catch (JSONException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			}
		}
	}
	
	class refreshMywowoThread extends Thread{
		private String uid;
		private int m;
		private boolean isrun = true;
		public refreshMywowoThread(String uid,int m) {
			super();
			this.uid = uid;
			this.m  = m;
		}
		@Override
		public void run() {
			while(isrun){
				isrun =false;
			Map<String, String> map = new HashMap<String, String>();
			map.put("uid", uid);
			map.put("lastrequest", DefaultValueSharePreference.getLastRequest(Mywowo.this));
			DefaultValueSharePreference.putLastRequest(Mywowo.this, String.valueOf(System.currentTimeMillis()/1000));
			String url = HttpUtil.BASE_URL + "space/list";
			try {
				//Log.i("af", HttpUtil.postRequest(url, map));
				JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(url, map));
				String result = jsonObj.getString("result");
				if(result.equals("1")){
					if(jsonObj.get("info").toString().length()>3){
						JSONArray mySpacelist =  jsonObj.getJSONObject("info").getJSONArray("mySpace");
						JSONArray joinedSpacelist =  jsonObj.getJSONObject("info").getJSONArray("joinedSpace");
						JSONArray invitedSpacelist =  jsonObj.getJSONObject("info").getJSONArray("invitedSpace");
						wowoList = new ArrayList<Space>();
						if(invitedSpacelist.length()>0){
							for(int i=0;i<invitedSpacelist.length();i++){
								Space list = new Space();
								list.setSpacetype("invitedSpace");
								list.setCount(invitedSpacelist.getJSONObject(i).getString("twitterCount"));
								list.setNumber(invitedSpacelist.getJSONObject(i).getString("memberCount"));
								list.setManagerCount(invitedSpacelist.getJSONObject(i).getString("managerCount"));
								list.setSpace_id(invitedSpacelist.getJSONObject(i).getString("space_id"));
								list.setNewMsg(invitedSpacelist.getJSONObject(i).getString("new"));
								list.setCheckMsg(false);
								list.setSpacename(StringEscapeUtils.unescapeHtml4(invitedSpacelist.getJSONObject(i).getString("spacename")));
								String cover = invitedSpacelist.getJSONObject(i).getString("cover");
								list.setCover(cover);
								list.setState(invitedSpacelist.getJSONObject(i).getString("state"));
								ArrayList<SpaceOauth> solist = new ArrayList<SpaceOauth>();
								for(int j =0;j<invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").length();j++){
									SpaceOauth so = new SpaceOauth();
									so.setNickname_fromuser(invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getJSONObject("from_user").getString("nickname"));
									so.setUid(invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("uid"));
									so.setCredits(invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("credits"));
									so.setRole(invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("role"));
									so.setCallname(invitedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("callname"));
									solist.add(so);
									}
								list.setSpaceoauth(solist);
								wowoList.add(list);
							}
						}
						if(mySpacelist.length()>0){
							for(int i=0;i<mySpacelist.length();i++){
								Space list = new Space();
								list.setSpacetype("mySpace");
								list.setCount(mySpacelist.getJSONObject(i).getString("twitterCount"));
								list.setNumber(mySpacelist.getJSONObject(i).getString("memberCount"));
								list.setManagerCount(mySpacelist.getJSONObject(i).getString("managerCount"));
								list.setTotalMemberCount(String.valueOf(Integer.valueOf(list.getNumber())+Integer.valueOf(list.getManagerCount())));
								list.setSpace_id(mySpacelist.getJSONObject(i).getString("space_id"));
								list.setNewMsg(mySpacelist.getJSONObject(i).getString("new"));
								list.setCheckMsg(false);
								list.setSpacename(StringEscapeUtils.unescapeHtml4(mySpacelist.getJSONObject(i).getString("spacename")));
								String cover = mySpacelist.getJSONObject(i).getString("cover");
								list.setCover(cover);
								list.setState(mySpacelist.getJSONObject(i).getString("state"));
								ArrayList<SpaceOauth> solist = new ArrayList<SpaceOauth>();
								for(int j =0;j<mySpacelist.getJSONObject(i).getJSONArray("spaceoauth").length();j++){
									SpaceOauth so = new SpaceOauth();
									so.setUid(mySpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("uid"));
									so.setCredits(mySpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("credits"));
									so.setRole(mySpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("role"));
									so.setCallname(mySpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("callname"));
									solist.add(so);
									}
								list.setSpaceoauth(solist);
								wowoList.add(list);
							}	
						}
						if(joinedSpacelist.length()>0){
							for(int i=0;i<joinedSpacelist.length();i++){
								Space list = new Space();
								list.setSpacetype("joinedSpace");
								list.setCount(joinedSpacelist.getJSONObject(i).getString("twitterCount"));
								list.setNumber(joinedSpacelist.getJSONObject(i).getString("memberCount"));
								list.setManagerCount(joinedSpacelist.getJSONObject(i).getString("managerCount"));
								list.setTotalMemberCount(String.valueOf(Integer.valueOf(list.getNumber())+Integer.valueOf(list.getManagerCount())));
								list.setSpace_id(joinedSpacelist.getJSONObject(i).getString("space_id"));
								list.setNewMsg(joinedSpacelist.getJSONObject(i).getString("new"));
								list.setCheckMsg(false);
								list.setSpacename(StringEscapeUtils.unescapeHtml4(joinedSpacelist.getJSONObject(i).getString("spacename")));
								String cover = joinedSpacelist.getJSONObject(i).getString("cover");
								list.setCover(cover);
								list.setState(joinedSpacelist.getJSONObject(i).getString("state"));
								ArrayList<SpaceOauth> solist = new ArrayList<SpaceOauth>();
								for(int j =0;j<joinedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").length();j++){
									SpaceOauth so = new SpaceOauth();
									so.setUid(joinedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("uid"));
									so.setCredits(joinedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("credits"));
									so.setRole(joinedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("role"));
									so.setCallname(joinedSpacelist.getJSONObject(i).getJSONArray("spaceoauth").getJSONObject(j).getString("callname"));
									solist.add(so);
									}
								list.setSpaceoauth(solist);
								wowoList.add(list);
							}
						}
						
						Message msg = mhandler.obtainMessage(m);
						msg.obj = wowoList;
						msg.sendToTarget();
					}
					else{
						Message msg = mhandler.obtainMessage(0);
						msg.sendToTarget();
					}
				}
				else if(result.equals("0")){
					Message msg = mhandler.obtainMessage(0);
					msg.obj = jsonObj.getString("msg");
					msg.sendToTarget();
				}
				else{
					Message msg = mhandler.obtainMessage(0);
					msg.obj = jsonObj.getJSONObject("info").getString("error");
					msg.sendToTarget();
				}
			} catch (JSONException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
			}
		}
	}
	public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
		position = position - 1;
		for(int i=0;i<mList.get(position).getSpaceoauth().size();i++){
			if(uid.equals(mList.get(position).getSpaceoauth().get(i).getUid()) ){
				mList.get(position).setCheckMsg(true);
				String id = mList.get(position).getSpace_id();
		    	String spacename = mList.get(position).getSpacename();
		    	String count = mList.get(position).getCount();
		        Intent intent = new Intent(Mywowo.this,Timeline.class);
		        intent.putExtra("sid", id);
		        intent.putExtra("uid", uid);
		        intent.putExtra("role", mList.get(position).getSpaceoauth().get(i).getRole());
		        intent.putExtra("spacename", spacename);
		        intent.putExtra("count", count);
		        intent.putExtra("cover", mList.get(position).getCover());
		        intent.putExtra("credits", mList.get(position).getSpaceoauth().get(i).getCredits());
		        intent.putExtra("member", mList.get(position).getNumber());
		        intent.putExtra("host", mList.get(position).getManagerCount());
		        intent.putExtra("state", mList.get(position).getState());
		        if(mList.get(position).getSpacetype().equals("invitedSpace"))
		        	intent.putExtra("accept", 1);
		        else
		        	intent.putExtra("accept", 0);
		        startActivity(intent);
			}
		}
	}
}
